/*
* @Author: Marte
* @Date:   2018-10-31 21:04:20
* @Last Modified by:   Marte
* @Last Modified time: 2018-10-31 21:04:23
*/
function Type(){
	this.init();
}

Type.TypeRowTemplate =`
	<tr>
		<td><%= _id %></td>
		<td class="type-dep"><%= department %></td>
		<td class="type-pro"><%= profession %></td>
		<td class="type-des"><%= describe %></td>
		<td><a href="" class="update" data-toggle="modal" data-target="#UpdateTypeModal">修改</a> <a href="javascript:void(0);" class="del">删除</a></td>
	</tr>

`;


$.extend(Type.prototype,{
	init(){
		this.change();
		this.addListener();
		this.loadData(1);
	},
	change(){
		$(".btn-type").css("background","#e6e6e6");
		
		$(".pagination").on("click", "a", $.proxy(this.loadDataHandler, this));
		
		$("tbody").on("click",".del",$.proxy(this.delDataHandler, this));
		
		$("tbody").on("click",".update",$.proxy(this.updateDataHandler, this));
	},
	addListener(){
		$(".btn-add-pos").on("click", this.addTypeHandler);
	},
	loadDataHandler(event) {
		const $src = $(event.target);
		const page = Number($src.text());
		this.loadData(page);

		$src.parent("li").addClass("active").siblings("li").removeClass("active");
	},

	loadData(page) {

		page = page || 1;

		const url = "/types/find_by_page?page=" + page;

		$.getJSON(url, (data) => {
			if (data.res_code === 1) {
				console.log(data.res_body);
				let html = "";
				data.res_body.list.forEach((curr)=>{
					html += ejs.render(Type.TypeRowTemplate, curr);
				});
				$(".table-type tbody").html(html);
			}
		});
	},	
	addTypeHandler(){

		const url = "/types/add";
		const data = $(".form-add-pos").serialize();
		$.post(url, data, (data)=>{
			if (data.res_body.status === 1) {
					const html = ejs.render(Type.TypeRowTemplate, data.res_body.data)
					//$(".table-type tbody").append(html);
					location.reload();
					$("#addTypeModal").modal("hide");
			} else {
				$(".add-pos-error").removeClass("hidden");
			}
		}, "json")		
	},
	delDataHandler(event){
		const $src = $(event.target);
		const profession=$src.parent("td").parent("tr").children(".type-pro").text();
		const url="/types/remove?profession="+profession;
		console.log(url);
		$.getJSON(url,(data)=>{
			if(data.res_code === 1){
				location.reload();
			}
			
		});		
		
	},
	updateDataHandler(event){
		const $src = $(event.target);
		const department=$src.parent("td").parent("tr").children(".type-dep").text();
		const profession=$src.parent("td").parent("tr").children(".type-pro").text();
		const describe=$src.parent("td").parent("tr").children(".type-des").text();
		
		$("#UpdateDepartment").val(department);
		$("#UpdateProfes").val(profession);
		$("#UpdateDescri").val(describe);
		
			$(".btn-update-type").on("click",function(){
				
				const url = "/types/update";
				const data = $(".form-update-type").serialize();
				
				$.post(url, data, (data)=>{
					if (data.res_body.status === 1) {
							location.reload();
							$("#addTypeModal").modal("hide");
					} else {
						$(".add-pos-error").removeClass("hidden");
					}
				}, "json")			
			});
		
				
		
	},	
});
new Type();
